//
//  CustomActionSheet.m
//  CustomActionSheetDemo
//
//  Created by wuleilei on 11-8-31.
//  Copyright 2011 http://www.wuleilei.com/ All rights reserved.
//

#import "CustomActionSheet.h"

@implementation CustomActionSheet

@synthesize view;
@synthesize toolBar;
@synthesize Customdelegate;

-(id)initWithHeight:(float)height WithSheetTitle:(NSString*)title WithDataArray:(NSArray *)dataArray{
	self = [super init];
    if (self) {
		int theight = height - 40;
		int btnnum = theight/50;
		for(int i=0; i<btnnum; i++){
			[self addButtonWithTitle:@" "];
		}
		toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0,320, 44)];
		toolBar.barStyle = UIBarStyleBlackOpaque;
		UIBarButtonItem *titleButton = [[UIBarButtonItem alloc] initWithTitle:title 
                                                                        style:UIBarButtonItemStylePlain 
                                                                       target:nil 
                                                                       action:nil];
		
		UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"完成" 
                                                                        style:UIBarButtonItemStyleDone 
                                                                       target:self
                                                                       action:@selector(done)];
		
		UIBarButtonItem *leftButton  = [[UIBarButtonItem alloc] initWithTitle:@"取消" 
                                                                        style:UIBarButtonItemStyleBordered 
                                                                       target:self 
                                                                       action:@selector(docancel)];
		
		UIBarButtonItem *fixedButton  = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace 
                                                                                      target:nil 
                                                                                      action:nil];
		
		NSArray *array = [[NSArray alloc] initWithObjects:leftButton,fixedButton,titleButton,fixedButton,rightButton,nil];
		[toolBar setItems: array];
		[titleButton release];
		[leftButton  release];
		[rightButton release];
		[fixedButton release];
		[array       release];
		[self addSubview:toolBar];
		view = [[UIView alloc] initWithFrame:CGRectMake(0, 44, 320, height-44)];
		view.backgroundColor = [UIColor groupTableViewBackgroundColor];
        
//        NSArray *dataArray = [[NSArray alloc]initWithObjects:@"许嵩",@"周杰伦",@"梁静茹",@"许飞",@"凤凰传奇",@"阿杜",@"方大同",@"林俊杰",@"胡夏",@"邱永传", nil];
        pickerData=dataArray;
        
        pickerView= [[UIPickerView alloc]initWithFrame:CGRectMake(0,0, 320, 216)];
        pickerView.delegate = self;
        pickerView.dataSource = self;
        pickerView.showsSelectionIndicator=YES;
        [view addSubview:pickerView];
		[self addSubview:view];
    }
    return self;
}

#pragma mark -
#pragma mark Picker Date Source Methods

//返回显示的列数
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}
//返回当前列显示的行数
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return [pickerData count];
}

#pragma mark Picker Delegate Methods

//返回当前行的内容,此处是将数组中数值添加到滚动的那个显示栏上
-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return [pickerData objectAtIndex:row];
}

//实例化方法
//参数为component的序号,返回被选中row的序号,若无row被选中,则返回-1
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    
}

-(void)done
{
    NSInteger row =[pickerView selectedRowInComponent:0];
    NSString *selected = [pickerData objectAtIndex:row];
//    NSString *message = [[NSString alloc] initWithFormat:@"你选择的是:%@",selected];
    
//    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
//                                                    message:message
//                                                   delegate:self
//                                          cancelButtonTitle:@"OK"
//                                          otherButtonTitles: nil];
//    [alert show];
	[self dismissWithClickedButtonIndex:0 animated:YES];
    if ([self.Customdelegate respondsToSelector:@selector(ClickItem:ActionSheetTag:)]) {
        [self.Customdelegate ClickItem:selected ActionSheetTag:self.tag];
    }
    
    if ([self.Customdelegate respondsToSelector:@selector(ClickItem:selectRow:ActionSheetTag:)]) {
		[self.Customdelegate ClickItem:selected selectRow:row ActionSheetTag:self.tag];
	}
}

-(void)docancel{
	[self dismissWithClickedButtonIndex:0 animated:YES];
}

-(void)dealloc{
    [toolBar release];
    [pickerView release];
	[view release];
	[super dealloc];
}

@end